﻿using System.Net.Sockets;
using System.Text;

namespace Zhaoxi.CommunicationLesson
{
    internal class Program
    {
        // 上位机主程序
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");


            #region 直接访问PLC
            //S7.Net.Plc plc = new S7.Net.Plc(S7.Net.CpuType.S7200Smart, "192.168.2.1", 0, 0);
            //plc.Open();


            // 不想主动请求    被动接收
            //Task.Run(() =>
            //{
            //    while (true)
            //    {
            //        var value = plc.Read("DB1.DBW0");
            //    }
            //});
            #endregion

            #region 通过服务访问PLC，并且返回数据
            // 客户端
            //Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //socket.Connect("127.0.0.1", 9091);


            //// 请求
            //string datas = "DB1.DBW0";
            //socket.Send(Encoding.UTF8.GetBytes(datas));

            //byte[] bytes = new byte[1024];
            //socket.Receive(bytes);

            //var str = Encoding.UTF8.GetString(bytes);
            #endregion


            #region 通过ServiceConnect对象进行数据传输    应答模式
            /// 已完成：
            /// 1、通过单次请求一个地址的数据
            /// 
            //ServiceConnect serviceConnect = new ServiceConnect();
            //if (serviceConnect.Connect("127.0.0.1", 9091))
            //{
            //    var value = serviceConnect.Read("DB1.DBW0", 10);

            //}
            #endregion


            #region 通过ServiceConnect对象进行数据传输    订阅模式
            ServiceConnect serviceConnect = new ServiceConnect();
            if (serviceConnect.Connect("127.0.0.1", 9091))
            {
                serviceConnect.DataReceived += ServiceConnect_DataReceived;
                serviceConnect.Subscribe("DB1.DBW0", 10);

            }
            #endregion

            Console.ReadLine();
        }

        private static void ServiceConnect_DataReceived(object? sender, object e)
        {

        }
    }
}